import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=572 lang=java
 *
 * [572] 另一棵树的子树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
       if(root==null&&subRoot!=null){
        return false;
       }
       if(root!=null&&subRoot==null){
        return false;
       }
       if(root==null&&subRoot==null){
        return true;
       }
       return isSameTree(root, subRoot)||isSubtree(root.left, subRoot)||isSubtree(root.right, subRoot);
    
    }

    public boolean isSameTree(TreeNode root,TreeNode subRoot){
        if(root==null&&subRoot!=null){
            return false;
        }else if(root!=null&&subRoot==null){
            return false;
        }else if(root==null&&subRoot==null){
            return true;
        }else if(root.val!=subRoot.val){
            return false;
        }
        boolean left=isSameTree(root.left, subRoot.left);
        boolean right=isSameTree(root.right, subRoot.right);
        return  left&&right;
    }
}

//熟练
// class Solution {
//     public boolean isSubtree(TreeNode root, TreeNode subRoot) {
//          if(root==null&&subRoot==null) return true;
//          if(root==null||subRoot==null) return false;
//         return isSameTree(root,subRoot)||isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);

//     }
//     public boolean isSameTree(TreeNode root1,TreeNode root2){
//         if(root1==null&&root2==null) return true;
//         if(root1==null||root2==null) return false;
//         return root1.val==root2.val&&isSameTree(root1.left,root2.left)&&isSameTree(root1.right,root2.right);
//     }
// }
// @lc code=end

